<?php
    include("header.php");
    setTabIndex(2);
    createPage("Liga - Zilveren Toren");
?>

<?php
    $connect = mysql_connect(DB_SERVER, DB_USER, DB_PWD);
    mysql_select_db(DB_CATALOG);
    
    $dep = $_GET['dep'];
    $playerKey = $_GET['playerKey'];

    $query = sprintf("select group_id from zt_group g
            join zt_tournament t on g.tournament_id = t.tournament_id
            where t.iscurrent = 1 and group_name = '%s'", $dep);
    $result = mysql_query($query, $connect);
    $row = mysql_fetch_assoc($result);
    $groupId = $row['group_id'];
    
    $query = sprintf("select nom_prenom, elo_calcul from playernew where matricule = %u", $playerKey);
    $result = mysql_query($query, $connect);
    $row = mysql_fetch_assoc($result);
    $playerName = $row['nom_prenom']." (".$row['elo_calcul'].")";

    $matches = array();
                                                
    $query = "select zp.player_key, p.matricule, p.nom_prenom, p.elo_calcul, 
        p.club, c.round_nr, rt.result
        from zt_player zp
        join zt_player_list zpl on zpl.player_list_id = zp.player_list_id
        join zt_tournament t on t.tournament_id = zpl.tournament_id
        join zt_game_player zgp on zgp.index_nr_home = zp.index_nr
        join zt_game g on g.game_id = zgp.game_id
        join zt_player zp2 on zp2.index_nr = zgp.index_nr_visit
        join zt_player_list zpl2 on zp2.player_list_id = zpl2.player_list_id
        join playernew p on p.matricule = zp2.player_key
        join zt_result_type rt on rt.result_type_id = zgp.result_type_id
        join zt_calendar c on c.calendar_id = g.calendar_id
        where t.iscurrent = 1
        and g.home_player_list_id = zpl.player_list_id
        and zpl2.player_list_id = g.visit_player_list_id
        and g.group_id = ".$groupId."
        and zp.player_key = '".$playerKey."'
        order by g.calendar_id desc";
    $homeMatchesResult = mysql_query($query, $connect);
    rejectTranIfError($query, "calculate_stats.php: selecteren van de thuispartijen is mislukt.");
    while ($homeMatchRow = mysql_fetch_assoc($homeMatchesResult)) {
        $homeMatch = new Match;
        $homeMatch->round = $homeMatchRow['round_nr'];
        $homeMatch->playerKey = $homeMatchRow['player_key'];
        $homeMatch->opponentClub = $homeMatchRow['club'];
        $homeMatch->opponentKey = $homeMatchRow['matricule'];
        $homeMatch->opponentName = $homeMatchRow['nom_prenom'];
        $homeMatch->opponentRating = $homeMatchRow['elo_calcul'];
        $homeMatch->result = $homeMatchRow['result'];
        $homeMatch->homeMatch = 1;
        array_push($matches, $homeMatch);    
    }
    
    $query = "select zp.player_key, p.matricule, p.nom_prenom, p.elo_calcul, 
        p.club, c.round_nr, rt.result
        from zt_player zp
        join zt_player_list zpl on zpl.player_list_id = zp.player_list_id
        join zt_tournament t on t.tournament_id = zpl.tournament_id
        join zt_game_player zgp on zgp.index_nr_visit = zp.index_nr
        join zt_game g on g.game_id = zgp.game_id
        join zt_player zp2 on zp2.index_nr = zgp.index_nr_home
        join zt_player_list zpl2 on zp2.player_list_id = zpl2.player_list_id
        join playernew p on p.matricule = zp2.player_key
        join zt_result_type rt on rt.result_type_id = zgp.result_type_id
        join zt_calendar c on c.calendar_id = g.calendar_id
        where t.iscurrent = 1
        and g.visit_player_list_id = zpl.player_list_id
        and zpl2.player_list_id = g.home_player_list_id
        and g.group_id = ".$groupId."
        and zp.player_key = '".$playerKey."'
        order by g.calendar_id desc";
    $visitMatchesResult = mysql_query($query, $connect);
    rejectTranIfError($query, "calculate_stats.php: selecteren van de uitpartijen is mislukt.");
    while ($visitMatchRow = mysql_fetch_assoc($visitMatchesResult)) {
        $visitMatch = new Match;
        $visitMatch->round = $visitMatchRow['round_nr'];
        $visitMatch->playerKey = $visitMatchRow['player_key'];
        $visitMatch->opponentClub = $visitMatchRow['club'];
        $visitMatch->opponentKey = $visitMatchRow['matricule'];
        $visitMatch->opponentName = $visitMatchRow['nom_prenom'];
        $visitMatch->opponentRating = $visitMatchRow['elo_calcul'];
        $visitMatch->result = $visitMatchRow['result'];
        $visitMatch->homeMatch = 0;
        array_push($matches, $visitMatch);    
    }                                     
?>

<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0" bgcolor="#FFFFFF">
	<tr>
		<td id="leftFiller" valign="top" height="100%" rowspan="3" bgcolor="#666666">
			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="8px">
				<tr><td>&nbsp;</td></tr>
			</table>
		</td>
		<td id="header" valign="top" width="100%">
			<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#10425b">
                <tr>
                    <td bgcolor="#666666" width="40%">
                        <?php include("inc_title.html"); ?>
                    </td>
                    <td bgcolor="#666666" align="right" valign="top">&nbsp;
                          <span class="headerSmall">Huidige versie PLAYER.DBF: <?=CURRENT_PLAYER_DBF?>
                        <?php
                        if (isset($user) && $user->club_id == 100) {
                        ?>
                        <br/>
                        <a class="headerSmall" href="import_player.php">laad andere versie</a>
                        <?php
                        }
                        ?>
                        </span>
                    </td>
                </tr>
            </table>
            <table align="center" border="0" width="100%" height="90%" cellpadding="0" cellspacing="0" style="border-top:#BCDD10 4px solid;">
                <tr>
                    <?php include("inc_sublinks.php"); ?> 
                </tr>
				<tr><td colspan="2" nowrap bgcolor="#BCDD10" height="4"><img src="../../assets/images/spacer.gif" alt="" height="1" width="1"></td></tr>
				<tr>
					<td>&nbsp;</td>
					<td valign="top">
						<table align="left" cellspacing="10" width="100%">
							<tr>
								<td>
                                  <span class="blackHeaderLarge">Zilveren Toren <?=CURRENT_ZT_YEAR?>
                                  - Afdeling <?=$dep?> - Spelersfiche <?=$playerName?></span>
                                  <br>&nbsp;<br>
                                  (Work in Progress)
                                  <br>&nbsp;<br>
                                    <?php
                                      $query = "select g.group_name from zt_group g join
                                        zt_tournament zt on zt.tournament_id = g.tournament_id
                                        where zt.isCurrent = 1";
                                      $result = mysql_query($query, $connect);
                                      while ($row = mysql_fetch_assoc($result)) {
                                          $groupName = $row['group_name'];
                                    ?>
                                    <a class="quicklink" href="zt_department.php?dep=<?=$groupName?>">Afdeling <?=$groupName?></a>|</li>
                                    <?php
                                      }
                                    ?>
                                    <br>&nbsp;<br>
                                    <center><a href="zt_department.php?dep=<?=$dep?>"><img border="0" src="../../assets/images/24x24/shadow/arrow_left_green.gif"></a></center>
                                    <br>
                                    
                                    <table border="0" width="70%" cellspacing="0" cellpadding="2" align="center">
                                        <tr>
                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Ronde</strong></td>
                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Stamnr</strong></td>
                                            <td width="35%" style="border-bottom:#DDDDDD solid 1px"><strong>Naam</strong></td>
                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Club</strong></td>
                                            <td width="10%" style="border-bottom:#DDDDDD solid 1px"><strong>Elo</strong></td>
                                            <td width="5%" style="border-bottom:#DDDDDD solid 1px"><strong>Res</strong></td>
                                        </tr>
                                    <?php
                                        foreach ($matches as $match) {
                                            $result = 0;
                                            if ($match->homeMatch == 1) {
                                                if (strnatcasecmp("1 - 0", $match->result) == 0) {
                                                    $result = 1;    
                                                } else if (strnatcasecmp("5 - 5", $match->result) == 0) {
                                                    $result = 0.5;
                                                } else if (strnatcasecmp("1 - FF", $match->result) == 0) {
                                                    $result = 1;
                                                }
                                            } else {
                                                if (strnatcasecmp("5 - 5", $match->result) == 0) {
                                                    $result = 0.5;    
                                                } else if (strnatcasecmp("0 - 1", $match->result) == 0) {
                                                    $result = 1;    
                                                } else if (strnatcasecmp("FF - 1", $match->result) == 0) {
                                                    $result = 1;   
                                                }
                                            }
                                    ?>
                                        <tr>
                                            <td style="border-bottom:#DDDDDD solid 1px"><?=$match->round?></td>
                                            <td style="border-bottom:#DDDDDD solid 1px"><?=$match->opponentKey?></td>
                                            <td style="border-bottom:#DDDDDD solid 1px"><a class="underline" href="player_detail.php?dep=<?=$dep?>&playerKey=<?=$match->opponentKey?>"><?=$match->opponentName?></a></td>
                                            <td style="border-bottom:#DDDDDD solid 1px"><?=$match->opponentClub?></td>
                                            <td style="border-bottom:#DDDDDD solid 1px"><?=$match->opponentRating?></td>
                                            <td style="border-bottom:#DDDDDD solid 1px"><?=$result?></td>
                                        </tr>
                                    <?php        
                                        }
                                    ?>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
			</table>
		</td>
	</tr>
</table>

<?php include("footer.php"); ?>
